package com.dl.study.module.base.authority.controller;

import com.dl.study.base.PageResultDTO;
import com.dl.study.util.resp.ResponseDTO;
import com.dl.study.module.base.authority.domain.dto.AuthorityDTO;
import com.dl.study.module.base.authority.domain.dto.AuthorityQueryDTO;
import com.dl.study.module.base.authority.domain.dto.AuthorityListQueryDTO;
import com.dl.study.module.base.authority.domain.vo.AuthorityAddVO;
import com.dl.study.module.base.authority.domain.vo.AuthorityUpdateVO;
import com.dl.study.module.base.authority.service.AuthorityService;
import com.dl.study.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * [ 权限表 ]
 *
 * @author dl
 * @version 1.0
 * @company xxxx
 * @copyright (c) 2019 xxxxInc. All rights reserved.
 * @date 2021-10-28 16:13:14
 * @since JDK1.8
 */
@RestController
@Api(tags = {"权限表" })
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @ApiOperation(value = "分页查询权限表", notes = "@author dl")
    @PostMapping("/authority/page/query")
    @RequiresRoles(value = "admin")
    public ResponseDTO<PageResultDTO<AuthorityDTO>> queryByPage(@RequestBody AuthorityQueryDTO queryDTO) {
        Subject subject = SecurityUtils.getSubject();
        return authorityService.queryByPage(queryDTO);
    }


    @ApiOperation(value = "根据条件查询权限表", notes = "@author dl")
    @PostMapping("/authority/list/query")
    public ResponseDTO<List<AuthorityDTO>> queryListByCondition(@RequestBody AuthorityListQueryDTO listQueryDTO) {
        return authorityService.queryListByCondition(listQueryDTO);
    }

    @ApiOperation(value = "添加权限表", notes = "@author dl")
    @PostMapping("/authority/add")
    public ResponseDTO<String> add(@RequestBody @Valid AuthorityAddVO addVO) {
        AuthorityDTO dto = BeanCopyUtil.copy(addVO, AuthorityDTO.class);
        return authorityService.add(dto);
    }

    @ApiOperation(value = "修改权限表", notes = "@author dl")
    @PostMapping("/authority/update")
    public ResponseDTO<String> update(@RequestBody @Valid AuthorityUpdateVO updateVO) {
        AuthorityDTO dto = BeanCopyUtil.copy(updateVO, AuthorityDTO.class);
        return authorityService.update(dto);
    }


    @ApiOperation(value = "删除权限表", notes = "@author dl")
    @GetMapping("/authority/delete/{id}")
    public ResponseDTO<String> delete(@PathVariable("id") Long id) {
        return authorityService.delete(id);
    }


    @ApiOperation(value = "详情权限表", notes = "@author dl")
    @GetMapping("/authority/detail/{id}")
    public ResponseDTO<AuthorityDTO> detail(@PathVariable("id") Long id) {
        return authorityService.detail(id);
    }


    @ApiOperation(value = "批量删除权限表", notes = "@author dl")
    @PostMapping("/authority/deleteByIds")
    public ResponseDTO<String> deleteByIds(@RequestBody @Valid List<Long> ids) {
        return authorityService.deleteByIds(ids);
    }


    @ApiOperation(value = "批量添加权限表", notes = "@author dl")
    @PostMapping("/authority/addList")
    public ResponseDTO<String> addList(@RequestBody @Valid List<AuthorityDTO> addList) {
        return authorityService.addList(addList);
    }
}
